package com.jwater.core.manager.proxy;

import java.util.Arrays;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jwater.core.common.InitLogger;
import com.jwater.core.common.JvmNames;
import com.jwater.core.manager.common.conf.CoreConfParse;
import com.jwater.core.metrics.JvmMetric;
import com.jwater.core.utils.ProcessUtils;
import com.jwater.launch.Jwater;

/**
 * @description: 代理进程入口类
 * @author: jnner
 * @createDate: 2021/2/5
 * @version: 1.0
 */
public class ProxyMain {
    /**
     * 强制杀死之前的进程
     *
     * @param logger
     */
    public static void killOld(Logger logger) {
        int currId = JvmMetric.pid();
        logger.info("currId:" + currId);
        CoreConfParse conf = new CoreConfParse();
        conf.parse();
        List<Integer> ids = ProcessUtils.getProcessIds(Jwater.class.getName(),
                JvmNames.getProxyName(conf.getClusterName()));
        for (Integer id : ids) {
            if (id != currId) {
                try {
                    ProcessUtils.killById(id, true);
                } catch (Exception e) {
                    logger.error(e.toString(), e);
                }
            }
        }
    }

    public static void main(String[] args) throws Exception {
        Logger logger = LoggerFactory.getLogger(ProxyMain.class);
        InitLogger.initProxy();
        logger.debug("proxy start:" + Arrays.toString(args));
        killOld(logger);
        new ProxyNode().start();
    }
}
